// @flow
import React, { Component } from 'react'
import styles from './Models.css'
// import { Link } from 'react-router-dom'
import ModelItem from 'components/ModelItem'

type Props = {
  data: Object,
  loading: boolean,
  match: Object,
  history: Object
}

const modelImages = {
  'PREPROCESSING': require('../../../public/models/TSTreeSplit1.png'),
  'EXTRACTION': require('../../../public/models/MoenR1.png'),
  'TRAINING': require('../../../public/models/autoplait1.png'),
  'SCORING': require('../../../public/models/autoplait1.png')
}
type States = {
  toggle: boolean
}
class Models extends Component {
  props: Props
  state: States
  panelToggle: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      toggle: true
    }
    this.panelToggle = this.panelToggle.bind(this)
  }

  panelToggle () {
    this.setState({
      toggle: !this.state.toggle
    })
  }
  render () {
    const display = (() => {
      if (this.state.toggle) {
        return 'block'
      }
      return 'none'
    })()

    const height = (() => {
      if (this.state.toggle) {
        return 'auto'
      }
      return '40px'
    })()
    const { data, history } = this.props
    const imgUrl = modelImages[data.name]
    return (
      <div className={`${styles['models-container']} animated fadeIn`} style={{ height }}>
        <h1>
          <a>{this.props.data.name}</a>
          <span onClick={this.panelToggle}>
            {
              this.state.toggle
                ? <i className='fa fa-angle-down' aria-hidden='true' />
                  : <i className='fa fa-angle-left' aria-hidden='true' />
            }
          </span>
        </h1>
        <div className={styles['models-container-content']} style={{ display }}>
          {
            this.props.data.algs.map((item, index) => {
              return <ModelItem key={`modelItem${index}`}
                imgUrl={imgUrl}
                match={this.props.match}
                history={history}
                data={item} loading={this.props.loading} />
            })
          }
        </div>
      </div>
    )
  }
}

export default Models
